﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.EntityModel.SchemaObjectModel;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;


using DZ.Data_Modals;  //CP@07.Dec14
using DZ.Data__Class;


namespace DZ.Forms
{
    /// <summary>
    /// Interaction logic for NewAntiBodyRegistration.xaml
    /// </summary>
    public partial class NewAntiBodyRegistration : Window
    {
        DB_DZA_Entities dbContext = new DB_DZA_Entities();
        DZAswCommon objASWCommon = new DZAswCommon();
        
        public int AntBID;
        //public NewAntiBodyRegistration()
        //{
        //    InitializeComponent();
          
        //    ////CP@07.Dec14_Start
        //    //TblAntibody objAntibody = new TblAntibody();
        //    //objAntibody.AntiBodyName = "Name2";
        //    //objAntibody.AntigenRetrival = "retirval2";
        //    //ClsAntibody objDbAntibody = new ClsAntibody();
        //    //objDbAntibody.Save(objAntibody);
        //    ////CP@07.Dec14_End

        //    Fill();
                     
        //}
        public NewAntiBodyRegistration(int AntID)
        {
            InitializeComponent();
            if (AntID > 0)
            {
                AntBID = AntID;
                Fill();
                FillData();
            }
            else
            {
                Fill();
            }
        }

        private void FillData()
        {
            try
            {
                //var innerJoinQuery =(from data in dbContext.TblAntibodies 
                //                    join Localizat in dbContext.TblCellLocalizarionMasters on data.LocalizationID  equals Localizat.LOCID_PK
                //                    join Rpc in dbContext.TblRPCMasters on data.RPCID equals Rpc.RPCID_PK
                //                    join clone in dbContext.TblCloneMasters on data.CloneID equals clone.CLOID_PK
                //                    join Source in dbContext.TblANBSourceMasters  on data.SourceID  equals Source.SourceID_PK 
                //                    join antigen in dbContext.TblAntigenMasters  on data.AntigenRetrival  equals antigen.AntigenID_PK 
                //                    where data.AnBID_PK ==AntID
                //                    select new { AntiBodyName = data.AntiBodyName, ShortDesc = data.ShortDesc, CatalogNumber = data.CatalogNumber, CompanyName = data.CompanyName, LotNo = data.LotNo, ExpiryDate =data.ExpiryDate,Localizat.LOCDescription,Rpc.RPCDescription,clone.CDescription ,Source.SDescription ,data.BarCodeVial,data.IncubationTime,data.H2O2IncubationTime,data.TargetBinderIncubationTime,data.PolyHRPIncubationTime,data.DABIncubationTime,data.HematoxylinIncubationTime,data.Reagent6IncubationTime,data.Reagent7IncubationTime,data.AntigenRetrival,data.CloneID,data.SourceID,data.RPCID,data.LocalizationID  }).FirstOrDefault();
                var innerJoinQuery = (from data in dbContext.TblAntibodies
                                      join AntDtails in dbContext.TblAntibodyDetails on data.AnBID_PK equals AntDtails.AntBDID_PK 
                                      where data.AnBID_PK == AntBID
                                      select new { AntiBodyName = data.AntiBodyName, ShortDesc = data.ShortDesc, CatalogNumber = data.CatalogNumber, CompanyName = AntDtails.Manufacture, LotNo = AntDtails.LotNo, ExpiryDate = AntDtails.ExpiryDate, data.BarCodeVial, data.IncubationTime, data.H2O2IncubationTime, data.TargetBinderIncubationTime, data.PolyHRPIncubationTime, data.DABIncubationTime, data.HematoxylinIncubationTime, data.Reagent6IncubationTime, data.Reagent7IncubationTime, data.AntigenRetrival, data.CloneID, data.SourceID, data.RPCID, data.LocalizationID }).FirstOrDefault();
                txtAntiBodyName.Text  = innerJoinQuery.AntiBodyName;
                txtAntiBodySName.Text = innerJoinQuery.ShortDesc;
                txtBarcodevial.Text = innerJoinQuery.BarCodeVial;
                txtLotNo.Text = innerJoinQuery.LotNo;
                //txtCompany.Text = innerJoinQuery.ma;

                cmbAntigenRetrieval.SelectedIndex  = innerJoinQuery.AntigenRetrival;
                cmbSource.SelectedIndex = innerJoinQuery.SourceID;
                cmbLocalization.SelectedIndex = innerJoinQuery.LocalizationID;
                cmbClone.SelectedIndex = innerJoinQuery.CloneID;
                cmbRecommendedPCtrl.SelectedIndex=innerJoinQuery.RPCID;
                cmbIncubationTime.SelectedIndex    =innerJoinQuery.IncubationTime;
                dtpExpiry.SelectedDate = innerJoinQuery.ExpiryDate;
                txtDAB.Text = innerJoinQuery.DABIncubationTime.ToString();
                txtH2O2.Text = innerJoinQuery.H2O2IncubationTime.ToString();
                txtHematoxylin.Text = innerJoinQuery.HematoxylinIncubationTime.ToString();
                txtCatalognumber.Text = innerJoinQuery.CatalogNumber.ToString();
                txtPolyHRP.Text = innerJoinQuery.PolyHRPIncubationTime.ToString();
                txtReagent6.Text = innerJoinQuery.Reagent6IncubationTime.ToString();
                txtReagent7.Text = innerJoinQuery.Reagent7IncubationTime.ToString();
                txtTargetBinder.Text = innerJoinQuery.TargetBinderIncubationTime.ToString();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void Fill()
        {
            try
            {
                //AntibodySource
                //TblANBSourceMaster objSource = new TblANBSourceMaster();
                var query = from data in dbContext.TblANBSourceMasters 
                orderby data.SourceID_PK 
                select data;
                DataItem diSource = new DataItem();
                diSource.Key = 0;
                diSource.Name = "Select";
                cmbSource.Items.Add(diSource);
                foreach (TblANBSourceMaster  details in query)
                {
                     diSource = new DataItem();
                    diSource.Key = details.SourceID_PK;
                    diSource.Name = details.SDescription ;
                    cmbSource .Items.Add(diSource);
                   
                 }
                cmbSource.SelectedIndex = 0;

                //Cell Localization
                //TblCellLocalizarionMaster objCellLocalizarion = new TblCellLocalizarionMaster();
                var Cellquery = from data in dbContext.TblCellLocalizarionMasters 
                            orderby data.LOCID_PK 
                            select data;
                DataItem diCell = new DataItem();
                diCell.Key = 0;
                diCell.Name = "Select";
                cmbLocalization.Items.Add(diCell);

                foreach (TblCellLocalizarionMaster  details in Cellquery)
                {
                    diCell = new DataItem();
                    diCell.Key = details.LOCID_PK ;
                    diCell.Name = details.LOCDescription ;
                    cmbLocalization .Items.Add(diCell);
                    
                }
                cmbLocalization.SelectedIndex = 0;

                //Recommended Positive Control
                //TblRPCMaster objRPC = new TblRPCMaster();
                var Rpcquery = from data in dbContext.TblRPCMasters 
                                orderby data.RPCID_PK 
                                select data;
                DataItem diRpc = new DataItem();
                diRpc.Key = 0;
                diRpc.Name = "Select";
                cmbRecommendedPCtrl.Items.Add(diRpc);

                foreach (TblRPCMaster details in Rpcquery)
                {
                    diRpc = new DataItem();
                    diRpc.Key = details.RPCID_PK;
                    diRpc.Name = details.RPCDescription;
                    cmbRecommendedPCtrl .Items.Add(diRpc);
                    
                }
                cmbRecommendedPCtrl.SelectedIndex = 0;

                //Incubation Time
                DataItem diIncubation = new DataItem();
                diIncubation.Key = 0;
                diIncubation.Name = "Select";
                cmbIncubationTime.Items.Add(diIncubation);


                diIncubation = new DataItem();
                diIncubation.Key = 1;
                diIncubation.Name = "30";
                cmbIncubationTime.Items.Add(diIncubation);

                diIncubation = new DataItem();
                diIncubation.Key = 2;
                diIncubation.Name = "60";
                cmbIncubationTime.Items.Add(diIncubation);

                cmbIncubationTime.SelectedIndex = 0;

                //Antigen Retrivel
                //TblRPCMaster objANR = new TblRPCMaster();
                var ANRquery = from data in dbContext.TblAntigenMasters 
                               orderby data.AntigenID_PK 
                               select data;
                DataItem diANR = new DataItem();
                diANR.Key = 0;
                diANR.Name = "Select";
                cmbAntigenRetrieval.Items.Add(diANR);

                foreach (TblAntigenMaster  details in ANRquery)
                {
                    diANR = new DataItem();
                    diANR.Key = details.AntigenID_PK ;
                    diANR.Name = details.AntigenDescription ;
                    cmbAntigenRetrieval .Items.Add(diANR);
                    
                }
                cmbAntigenRetrieval.SelectedIndex = 0;
                //Clone
                var Clonequery = from data in dbContext.TblCloneMasters 
                               orderby data.CLOID_PK 
                               select data;
                DataItem diClone = new DataItem();
                diClone.Key = 0;
                diClone.Name = "Select";
                cmbClone.Items.Add(diClone);

                foreach (TblCloneMaster  details in Clonequery)
                {
                    diClone = new DataItem();
                    diClone.Key = details.CLOID_PK ;
                    diClone.Name = details.CDescription  ;
                    cmbClone .Items.Add(diClone);
                   
                }
                cmbClone.SelectedIndex = 0;

            }
            catch { }
        }

        private void bntSave_Click(object sender, RoutedEventArgs e)
        {

            try
            {

            //CP@07.Dec14_Start
            TblAntibody objAntibody = new TblAntibody();
            TblAntibodyDetail objAntibodyDetails = new TblAntibodyDetail();
            objAntibody.AntiBodyName = (txtAntiBodyName.Text);
            objAntibody.ShortDesc =  (txtAntiBodySName.Text);
            objAntibody.SourceID = objASWCommon.GetComboValueItem(ref cmbSource); //cmbSource.SelectedIndex;
            objAntibody.CatalogNumber = (txtCatalognumber.Text);
            objAntibodyDetails.Manufacture  = "";
            objAntibodyDetails.LotNo = "";
            objAntibodyDetails.IsDeleted = "";
            //objAntibodyDetails.ExpiryDate = "";
            objAntibody.LocalizationID = objASWCommon.GetComboValueItem(ref cmbLocalization );// (cmbLocalization.SelectedIndex);
            objAntibody.RPCID = objASWCommon.GetComboValueItem(ref cmbRecommendedPCtrl); //cmbRecommendedPCtrl.SelectedIndex;
            objAntibody.BarCodeVial = txtBarcodevial.Text;
            objAntibody.IncubationTime = objASWCommon.GetComboValueItem(ref cmbIncubationTime); //cmbIncubationTime.SelectedIndex;
            objAntibody.AntigenRetrival = objASWCommon.GetComboValueItem(ref cmbAntigenRetrieval); //cmbAntigenRetrieval.SelectedIndex;
            objAntibody.CloneID = objASWCommon.GetComboValueItem(ref cmbAntigenRetrieval );
            objAntibody.H2O2IncubationTime = objASWCommon.CheckEmptyTextBox(ref txtH2O2);
            objAntibody.TargetBinderIncubationTime = objASWCommon.CheckEmptyTextBox(ref txtTargetBinder);
            objAntibody.PolyHRPIncubationTime = objASWCommon.CheckEmptyTextBox(ref txtPolyHRP);
            objAntibody.DABIncubationTime = objASWCommon.CheckEmptyTextBox(ref txtDAB);
            objAntibody.HematoxylinIncubationTime = objASWCommon.CheckEmptyTextBox(ref txtHematoxylin);
            objAntibody.Reagent6IncubationTime = objASWCommon.CheckEmptyTextBox(ref txtReagent6);
            objAntibody.Reagent7IncubationTime = objASWCommon.CheckEmptyTextBox(ref txtReagent7);
                           
            objAntibody.IsDeleted = "N";
            objAntibody.IsRegister = "N";

            ClsAntibody objDbAntibody = new ClsAntibody();
            objDbAntibody.Save(objAntibody);
            objDbAntibody.SaveDetails(objAntibodyDetails);
            
            objASWCommon.ShowMessage("Data Saved Sucessfully ");
            //this.Close();
            
            //CP@07.Dec14_End
            }
            catch{}
           
        }

        private void bntRegister_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                TblAntibody objAntibody = new TblAntibody();
                ClsAntibody objDbAntibody = new ClsAntibody();
                TblAntibodyDetail objAntibodyDetails = new TblAntibodyDetail();
                using (var ctx = new DB_DZA_Entities())
                {
                    objAntibody = ctx.TblAntibodies.Where(s => s.AntiBodyName == txtAntiBodyName.Text).FirstOrDefault<TblAntibody>();
                }
                if (objAntibody == null)
                {
                    objASWCommon.ShowMessage("Please Save Antibody Data before Register");
                    return;
                }
               

                var query = from data in dbContext.TblAntibodies
                            orderby data.AnBID_PK
                            where data.AnBID_PK == objAntibody.AnBID_PK && data.AntiBodyName == objAntibody.AntiBodyName
                            select data;
                              
                foreach (TblAntibody details in query)
                {
                    if (details.AnBID_PK == objAntibody.AnBID_PK && details.AntiBodyName==objAntibody.AntiBodyName )
                    {
                        //Assign the new values to name whose id is 1

                        objAntibody.H2O2IncubationTime = objASWCommon.CheckEmptyTextBox(ref txtH2O2 );
                        objAntibody.TargetBinderIncubationTime = objASWCommon.CheckEmptyTextBox(ref txtTargetBinder);
                        objAntibody.PolyHRPIncubationTime = objASWCommon.CheckEmptyTextBox(ref txtPolyHRP);
                        objAntibody.DABIncubationTime = objASWCommon.CheckEmptyTextBox(ref txtDAB );
                        objAntibody.HematoxylinIncubationTime = objASWCommon.CheckEmptyTextBox(ref txtHematoxylin);
                        objAntibody.Reagent6IncubationTime = objASWCommon.CheckEmptyTextBox(ref txtReagent6);
                        objAntibody.Reagent7IncubationTime = objASWCommon.CheckEmptyTextBox(ref txtReagent7);
                        objAntibody.IsRegister = "Y";

                        objAntibodyDetails.LotNo = (txtLotNo.Text);
                        objAntibodyDetails.Manufacture =(txtCompany.Text );
                        objAntibodyDetails.ExpiryDate = Convert.ToDateTime((dtpExpiry.DisplayDate));
                        objAntibody.IsDeleted = "N";
                    }

                }

                //objDbAntibody.Save(objAntibody);
                //objDbAntibody
                objDbAntibody.Update(objAntibody );
                objDbAntibody.UpdateDetails(objAntibodyDetails);
                objASWCommon.ShowMessage("Antibody Registered Sucessfully ");
                this.Close();
            }
            catch { }
        }

        private void bntCancel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }   
        }

        private void bntFCancel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }   
        }

        private void bntClose_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }   
        }

    }
}
